﻿using DataServer.Entities;
using IsPay.Logic;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IsPay.View
{
    public partial class EditAccunt : System.Web.UI.Page
    {
        private String returnmsge = "";
        public Accunt accunt = new Accunt();
        public IEnumerable<Accunt> lsaccunt = new List<Accunt>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] == null || Session["USERID"].ToString().Length <= 0)
            {
                Response.Write("<script>top.location='../Default.aspx'</script>");
            }
            if (Request.Params["ActionType"] != null)
            {
                String actionType = Request.Params["ActionType"].ToString();
                if (actionType.Equals("addsingle"))
                {
                    returnmsge = addAccuntSingle();
                    lblmsg.Text = returnmsge;
                }
                else if (actionType.Equals("query"))
                {
                    String strqaccuntid = Request.Params["txtAccuntid"].ToString().Trim();
                    String strqaccuntname = Request.Params["txtAccuntname"].ToString().Trim();
                    String strisactive = Request.Params["ISACTIVE"] != null ? Request.Params["ISACTIVE"].ToString().Trim() : "False";
                    AccuntSer aser = new AccuntSer();
                    lsaccunt = aser.QueryAccunt(strqaccuntid, strqaccuntname, strisactive);

                }
                else if (actionType.Equals("modify"))
                {
                    String edittablejasn = Request.Params["updatejason"].ToString().Trim();
                    DataTable dtedit = JasonToDataTable(edittablejasn);
                    if (dtedit != null && dtedit.Rows.Count > 0)
                    {
                        try
                        {
                            List<Accunt> lsaccunt = DataTableToAccunt(dtedit);
                            AccuntSer atedit = new AccuntSer();
                            String strresoult = atedit.ModifyAccunt(lsaccunt, Session["USERID"].ToString());
                            if (strresoult.Equals("OK"))
                            {
                                lblmsg.Text = "更新成功";
                            }
                            else
                            {
                                lblmsg.Text = strresoult;
                            }

                        }
                        catch (Exception ex)
                        {
                            lblmsg.Text = ex.Message;
                        }
                    }



                }
            }
        }
        private DataTable JasonToDataTable(String edittablejasn)
        {
            JArray ja = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(edittablejasn);
            int irow = 0;
            DataTable dt = new DataTable();
            foreach (JObject jobj in ja)
            {

                if (irow == 0)
                {
                    foreach (JProperty pry in jobj.Properties())
                    {
                        dt.Columns.Add(pry.Name.ToUpper());
                    }

                }
                DataRow drnew = dt.NewRow();
                foreach (JProperty pryv in jobj.Properties())
                {
                    drnew[pryv.Name.ToUpper()] = pryv.Value;
                }
                dt.Rows.Add(drnew);
                irow++;
            }
            return dt;
        }
        private List<Accunt> DataTableToAccunt(DataTable dtuser)
        {
            List<Accunt> lsAccunt = new List<Accunt>();
            foreach (DataRow dr in dtuser.Rows)
            {
                Accunt at = new Accunt();
                at.AccuntId = dr["ACCUNTID"].ToString().Trim();
                at.AccuntName = dr["ACCUNTNAME"].ToString().Trim();
                at.AccuntPayPWD = dr["ACCUNTPAYPWD"].ToString().Trim();

                at.AccuntPayPWD = dr["ACCUNTPAYPWD"].ToString().Trim().Length > 0 ? CodeMange.getMD5StringCode(dr["ACCUNTPAYPWD"].ToString().Trim()) : "";
                if (dr["deleteflag"].ToString().Equals("True"))
                {
                    at.IsActive = "0";
                }
                else
                {
                    at.IsActive = "1";
                }
                at.LastModifyer = Session["USERID"].ToString();
                at.LastModifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                lsAccunt.Add(at);
            }

            return lsAccunt;
        }
        private String addAccuntSingle()
        {
            String strmsg = "添加完成";
            AccuntSer checkaccunt = new AccuntSer();
            accunt.AccuntId = Request.Params["txtAccuntId"].ToString().Trim();
            accunt.AccuntName = Request.Params["txtAccuntName"].ToString().Trim();
           //accunt.AccuntPayPWD = CodeMange.getMD5StringCode("123");
            accunt.IsActive = "1";

            if (accunt.AccuntId == null || accunt.AccuntId.Length <= 0)
            {

                lblmsg.Text = "请填写账户ID";
                return "请填写账户ID";
            }
            else
            {


            }

            if (accunt.AccuntName == null || accunt.AccuntName.Length <= 0)
            {

                lblmsg.Text = "请填写账户名";
                return "请填写账户名";

            }
            else
            {

            }

            if (Request.Params["txtAccuntPayPWD"] == null || Request.Params["txtAccuntPayPWD"].ToString().Trim().Length <= 0)
            {
                lblmsg.Text = "请输入初始密码";
                return "请输入初始密码";
            }
            else
            {
                //byte[] result = Encoding.Default.GetBytes(Request.Params["txtusepwd1"].ToString().Trim());
                //MD5 md5 = new MD5CryptoServiceProvider();
                //byte[] output = md5.ComputeHash(result);
                //String strMD5pwd = BitConverter.ToString(output).Replace("-", "");
                //user.Pwd = strMD5pwd;
                accunt.AccuntPayPWD = CodeMange.getMD5StringCode(Request.Params["txtAccuntPayPWD"].ToString().Trim());

            }


            if (checkaccunt.CheckAccuntDouble("AccuntId", accunt.AccuntId))
            {
                lblmsg.Text = "用户已经存在";
                return "用户已经存在"; ;
            }
            if (checkaccunt.CheckAccuntDouble("AccuntName", accunt.AccuntName))
            {
                lblmsg.Text = "用户名已经存在";
                return "用户名已经存在";
            }
            accunt.LastModifyer = Session["USERID"].ToString();
            accunt.LastModifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (checkaccunt.AddAccunt(accunt))
            {
                strmsg = "添加成功";
            }
            else
            {
                strmsg = "添加失败";
            }

            return strmsg;
        }
    }
}